{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Convert an Array Into a 2D Array With Conditions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findMatrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #转换二维数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> 。请你创建一个满足以下条件的二维数组：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>二维数组应该 <strong>只</strong> 包含数组 <code>nums</code> 中的元素。</li>\n",
    "\t<li>二维数组中的每一行都包含 <strong>不同</strong> 的整数。</li>\n",
    "\t<li>二维数组的行数应尽可能 <strong>少</strong> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回结果数组。如果存在多种答案，则返回其中任何一种。</p>\n",
    "\n",
    "<p>请注意，二维数组的每一行上可以存在不同数量的元素。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,3,4,1,2,3,1]\n",
    "<strong>输出：</strong>[[1,3,4,2],[1,3],[1]]\n",
    "<strong>解释：</strong>根据题目要求可以创建包含以下几行元素的二维数组：\n",
    "- 1,3,4,2\n",
    "- 1,3\n",
    "- 1\n",
    "nums 中的所有元素都有用到，并且每一行都由不同的整数组成，所以这是一个符合题目要求的答案。\n",
    "可以证明无法创建少于三行且符合题目要求的二维数组。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2,3,4]\n",
    "<strong>输出：</strong>[[4,3,2,1]]\n",
    "<strong>解释：</strong>nums 中的所有元素都不同，所以我们可以将其全部保存在二维数组中的第一行。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 200</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [convert-an-array-into-a-2d-array-with-conditions](https://leetcode.cn/problems/convert-an-array-into-a-2d-array-with-conditions/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [convert-an-array-into-a-2d-array-with-conditions](https://leetcode.cn/problems/convert-an-array-into-a-2d-array-with-conditions/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,3,4,1,2,3,1]', '[2,1,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        d=dict(Counter(nums))\n",
    "        c=max(d.values())\n",
    "        w=[]\n",
    "        for i in range(c):\n",
    "            s=[]\n",
    "            for k in d:\n",
    "                if d[k]>0:\n",
    "                    s.append(k)\n",
    "                    d[k]-=1\n",
    "            w.append(s)\n",
    "        return w\n",
    "\n",
    "            \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        c = Counter(nums)\n",
    "        c2 = Counter(c.keys())\n",
    "        ans = []\n",
    "        while c:\n",
    "            ans.append(list(c))\n",
    "            c -= c2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        while nums:\n",
    "            s = list(set(nums))\n",
    "            ans.append(s)\n",
    "            for i in s:\n",
    "                nums.remove(i)\n",
    "        return ans \n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        nums.sort()\n",
    "        res = [[nums[0]]]\n",
    "        max_ = tmp = 0\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] == nums[i-1]:\n",
    "                tmp += 1\n",
    "                if tmp > max_:\n",
    "                    res += [[]]\n",
    "                    max_ = tmp\n",
    "            else:\n",
    "                tmp = 0\n",
    "            res[tmp].append(nums[i])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = [[]]\n",
    "        line_count = 1\n",
    "        for i in nums:\n",
    "            line = 0\n",
    "            while i in ans[line]:\n",
    "                line += 1\n",
    "                if line == line_count:\n",
    "                    ans.append([])\n",
    "                    line_count += 1\n",
    "            ans[line].append(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        d = defaultdict(int)\n",
    "        m = 0\n",
    "        for i in nums:\n",
    "            d[i]+=1\n",
    "            m = max(m,d[i])\n",
    "        ans = [[] for i in range(m)]\n",
    "        for i,j in d.items():\n",
    "            for k in range(j):\n",
    "                ans[k].append(i)\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        cnt = Counter(nums)\n",
    "        ans = []\n",
    "        while True:\n",
    "            tmp = []\n",
    "            for k, v in cnt.items():\n",
    "                if v:\n",
    "                    tmp.append(k)\n",
    "                    cnt[k] -= 1\n",
    "            if not tmp: return ans\n",
    "            ans.append(tmp)\n",
    "        return []\n",
    "                \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        cnt=Counter(nums)\n",
    "        ans=[[] for _ in range(max(cnt.values()))]\n",
    "        for n in cnt:\n",
    "            for i in range(cnt[n]):\n",
    "                ans[i].append(n)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        cnt = Counter(nums)\n",
    "        ans = [[] for _ in range(max(cnt.values()))]\n",
    "        for x,c in cnt.items():\n",
    "            for i in range(c): ans[i].append(x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        a = sorted(list(collections.Counter(nums).items()), key=lambda x:x[1], reverse=True)\n",
    "        a = [list(i) for i in a]\n",
    "        res = []\n",
    "        while a[0][1] > 0:\n",
    "            c = [j[0] for j in a if j[1] >= 1]\n",
    "            res.append(c)\n",
    "            for i in a:\n",
    "                i[1] -= 1\n",
    "        return res\n",
    "                            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        cnts = Counter(nums)\n",
    "        max_cnt = cnts.most_common(1)[0][1]\n",
    "        ans = [[] for _ in range(max_cnt)]\n",
    "        for num, cnt in cnts.items():\n",
    "            for i in range(cnt):\n",
    "                ans[i].append(num)\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        c = Counter(nums)\n",
    "        row = max(c.values())\n",
    "        res = [[] for _ in range(row)]\n",
    "        for num in c:\n",
    "            idx = 0\n",
    "            for _ in range(c[num]):\n",
    "                res[idx].append(num)\n",
    "                idx += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        counter = {}\n",
    "        for num in nums:\n",
    "            counter[num] = counter.get(num, 0) + 1\n",
    "\n",
    "        res = []\n",
    "        while counter:\n",
    "            res.append(list(counter.keys()))\n",
    "            # update\n",
    "            for k in counter:\n",
    "                counter[k] -= 1\n",
    "            counter = {k: v for k, v in counter.items() if v > 0}\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        d = {}\n",
    "        for x in nums:\n",
    "            if x in d:\n",
    "                d[x] += 1\n",
    "            else:\n",
    "                d[x] = 1\n",
    "        a = []\n",
    "        n = len(d)\n",
    "        while n > 0:\n",
    "            t = []\n",
    "            for x in d:\n",
    "                if d[x] > 0:\n",
    "                    t.append(x)\n",
    "                    d[x] -= 1\n",
    "                    if d[x] == 0:\n",
    "                        n -= 1\n",
    "            a.append(t)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        n = len(nums)\n",
    "        cnt = Counter(nums)\n",
    "        res = []\n",
    "        while cnt:\n",
    "            res.append(list(cnt))\n",
    "            for x in res[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "\n",
    "        cnt = Counter(nums)\n",
    "\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        d = {}\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in d:\n",
    "                d[nums[i]] = 0\n",
    "            d[nums[i]] += 1\n",
    "        ans = []\n",
    "        count = 0\n",
    "        while count < len(nums):\n",
    "            l = []\n",
    "            for i in d:\n",
    "                if d[i] > 0:\n",
    "                    l.append(i)\n",
    "                    d[i] -= 1\n",
    "            count += len(l)\n",
    "            ans.append(l)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "      counter = Counter(nums)\n",
    "      most_common = counter.most_common()\n",
    "      answer = []\n",
    "\n",
    "      for tp in most_common:\n",
    "        for i in range(tp[1]):\n",
    "          if i >= len(answer):\n",
    "            answer.append([tp[0]])\n",
    "          else:\n",
    "            answer[i].append(tp[0])\n",
    "      return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        Res = []\n",
    "        while nums:\n",
    "            res = []\n",
    "            newNums = []\n",
    "            numSet = set()\n",
    "            for num in nums:\n",
    "                if num not in numSet:\n",
    "                    res.append(num)\n",
    "                    numSet.add(num)\n",
    "                else:\n",
    "                    newNums.append(num)\n",
    "            Res.append(res)\n",
    "            nums = newNums\n",
    "        return Res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        cnts = collections.Counter(nums)\n",
    "        max_ = max(cnts.values())\n",
    "        res = [[] for _ in range(max_)]\n",
    "\n",
    "        for key, val in cnts.items():\n",
    "            for i in range(val):\n",
    "                res[i].append(key)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "      counter = Counter(nums)\n",
    "      most_common = counter.most_common()\n",
    "      answer = []\n",
    "      row = most_common[0][1]\n",
    "\n",
    "      for tp in most_common:\n",
    "        for i in range(tp[1]):\n",
    "          if i >= len(answer):\n",
    "            answer.append([tp[0]])\n",
    "          else:\n",
    "            answer[i].append(tp[0])\n",
    "      return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        numbers = sorted(nums)\n",
    "        m = 0\n",
    "        c = 0\n",
    "        for i in range(len(numbers)):\n",
    "            c +=1\n",
    "            if i+1 < len(numbers) and numbers[i+1] != numbers[i]:\n",
    "                m = max(m, c)\n",
    "                c = 0\n",
    "        m = max(m,c)\n",
    "        res = [[] for _ in range(m)]\n",
    "        c = 0\n",
    "        for i in range(len(numbers)):\n",
    "            res[c].append(numbers[i])\n",
    "            if i+1 < len(numbers) and numbers[i+1] != numbers[i]:\n",
    "                c = 0\n",
    "            else:\n",
    "                c +=1\n",
    "        return res \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        n = len(nums)\n",
    "        cnt = Counter(nums)\n",
    "        res = []\n",
    "        while True:\n",
    "            a = []\n",
    "            for k in cnt:\n",
    "                if cnt[k] > 0:\n",
    "                    a.append(k)\n",
    "                    cnt[k] -= 1\n",
    "            if len(a) == 0: return res\n",
    "            res.append(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        results = []\n",
    "        for num in nums:\n",
    "            flag = False\n",
    "            for result in results:\n",
    "                if num not in result:\n",
    "                    result.append(num)\n",
    "                    flag = True\n",
    "                    break\n",
    "            if not flag:\n",
    "                results.append([num])\n",
    "        return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        res = [set()]\n",
    "        for n in nums:\n",
    "            flag = False\n",
    "            for s in res:\n",
    "                if n not in s:\n",
    "                    s.add(n)\n",
    "                    flag = True\n",
    "                    break\n",
    "            if not flag:\n",
    "                a = set()\n",
    "                a.add(n)\n",
    "                res.append(a)\n",
    "        return [list(x) for x in res]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            cnt.subtract(ans[-1])\n",
    "            cnt = +cnt\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        dic = dict()\n",
    "        for i in nums:\n",
    "            if i in dic:\n",
    "                dic[i] += 1\n",
    "            else:\n",
    "                dic[i] = 1\n",
    "        cnt = []\n",
    "        for i in dic:\n",
    "            cnt.append(dic[i])\n",
    "        \n",
    "        ans = [[] for _ in range(max(cnt))]\n",
    "         \n",
    "        for i in dic:\n",
    "            for j in range(dic[i]):\n",
    "                ans[j].append(i)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        # c = Counter(nums)\n",
    "        # row = max(c.values())\n",
    "        # res = [[] for _ in range(row)]\n",
    "        # for num in c:\n",
    "        #     idx = 0\n",
    "        #     for _ in range(c[num]):\n",
    "        #         res[idx].append(num)\n",
    "        #         idx += 1\n",
    "        # return res\n",
    "\n",
    "        # -----------------------------------\n",
    "        res = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            res.append(list(cnt))\n",
    "            for x in res[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        from collections import Counter\n",
    "        count = Counter(nums)\n",
    "        res = []\n",
    "        while count:\n",
    "            cur = []\n",
    "            keys = list(count.keys())\n",
    "            for k in keys:\n",
    "                cur.append(k)\n",
    "                count[k] -= 1\n",
    "                if count[k] == 0:\n",
    "                    count.pop(k)\n",
    "            res.append(cur)\n",
    "\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:del cnt[x]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        nums.sort()\n",
    "        results = [[]]\n",
    "        idx = 0\n",
    "        cur = None\n",
    "        for num in nums:\n",
    "            if cur != num:\n",
    "                idx = 0\n",
    "            if idx < len(results):\n",
    "                results[idx].append(num)\n",
    "            else:\n",
    "                results.append([num])\n",
    "            idx += 1\n",
    "            cur = num\n",
    "        return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        c = Counter(nums)\n",
    "        ans = []\n",
    "        while c:\n",
    "            ans.append(list(c))\n",
    "            c -= Counter(ans[-1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        cnt = defaultdict(int)\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            cnt[nums[i]] += 1\n",
    "        arr = []\n",
    "        for k, v in cnt.items():\n",
    "            arr.append([k,v])\n",
    "        m = len(arr)\n",
    "        ans = []\n",
    "        while m > 0:\n",
    "            ans.append([])\n",
    "            for i in range(len(arr)):\n",
    "                if arr[i][1]:\n",
    "                    ans[-1].append(arr[i][0])\n",
    "                    arr[i][1] -= 1\n",
    "                    if arr[i][1] == 0:\n",
    "                        m -= 1\n",
    "        if not ans[-1]:\n",
    "            ans.pop()\n",
    "        return ans \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums):\n",
    "        dic = Counter(nums)\n",
    "        mx = 0\n",
    "        for k, v in dic.items():\n",
    "            mx = max(mx, v)\n",
    "\n",
    "        ret = [[] for _ in range(mx)]\n",
    "        for k, v in dic.items():\n",
    "            for i in range(v):\n",
    "                ret[i].append(k)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        cnt = Counter()\n",
    "        n = 0\n",
    "        for num in nums:\n",
    "            cnt[num] += 1\n",
    "            n = max(n, cnt[num])\n",
    "        ans = [list() for _ in range(n)]\n",
    "        for key in cnt.keys():\n",
    "            for i in range(cnt[key]):\n",
    "                ans[i].append(key)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        d={}\n",
    "        for i,n in enumerate(nums):\n",
    "            if n in d:\n",
    "                d[n]+=1\n",
    "            else:\n",
    "                d[n]=1\n",
    "        d=list(d.items())\n",
    "        d.sort(key=lambda x:x[1],reverse=True)\n",
    "        ans=[]\n",
    "        m=[]\n",
    "        for i in range(len(d)):\n",
    "            m.append([d[i][0],d[i][1]])\n",
    "        for i in range(d[0][1]):\n",
    "            temp=[]\n",
    "            for j in range(len(d)):\n",
    "                if m[j][1]>0:\n",
    "                    temp.append(m[j][0])\n",
    "                    m[j][1]-=1\n",
    "            ans.append(temp)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        dic = dict()\n",
    "        for i in nums:\n",
    "            if i in dic:\n",
    "                dic[i] += 1\n",
    "            else:\n",
    "                dic[i] = 1\n",
    "        cnt = []\n",
    "        for i in dic:\n",
    "            cnt.append(dic[i])\n",
    "        \n",
    "        ans = [[] for _ in range(max(cnt))]#[[]*max(cnt)]!wrong\n",
    "         \n",
    "        for i in dic:\n",
    "            for j in range(dic[i]):\n",
    "                ans[j].append(i)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        from collections import defaultdict\n",
    "        dic = defaultdict(int)\n",
    "        for n in nums:\n",
    "            dic[n] += 1\n",
    "        res = []\n",
    "        while dic:\n",
    "            tmp = []\n",
    "            dk = list(dic.keys())\n",
    "            for k in dk:\n",
    "                dic[k] -= 1\n",
    "                if dic[k] == 0:\n",
    "                    del dic[k]\n",
    "                tmp.append(k)\n",
    "            res.append(tmp)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        n = len(nums)\n",
    "        nums = sorted(nums)\n",
    "        ans = [[]]\n",
    "        idx = 0\n",
    "        while idx < n:\n",
    "            num = nums[idx]\n",
    "            idx2 = 0\n",
    "            while num in ans[idx2]:\n",
    "                idx2 += 1\n",
    "                if idx2 >= len(ans)-1:\n",
    "                    ans.append([])\n",
    "            ans[idx2].append(num)\n",
    "            idx+=1\n",
    "        if ans[-1] == []:\n",
    "            ans.pop()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        res, cnt = defaultdict(list), defaultdict(int)\n",
    "        for x in nums:\n",
    "            res[cnt[x]].append(x)\n",
    "            cnt[x] += 1\n",
    "        return list(res.values())\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        results = []\n",
    "        for num in nums:\n",
    "            for result in results:\n",
    "                if num not in result:\n",
    "                    result.add(num)\n",
    "                    break\n",
    "            else:\n",
    "                results.append(set([num]))\n",
    "        results = [list(num_set) for num_set in results]\n",
    "        return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: list[int]) -> list[list[int]]:\n",
    "        a=collections.Counter(nums)\n",
    "        res=[]\n",
    "        cnt=0\n",
    "        while cnt<len(nums):\n",
    "            tmp=[]\n",
    "            for i in a:\n",
    "                if a[i]==0:\n",
    "                    pass\n",
    "                else:\n",
    "                    a[i]-=1\n",
    "                    tmp.append(i)\n",
    "                    cnt+=1\n",
    "            res.append(tmp)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        results = []\n",
    "        for num in nums:\n",
    "            for result in results:\n",
    "                if num not in result:\n",
    "                    result.append(num)\n",
    "                    break\n",
    "            else:\n",
    "                results.append([num])\n",
    "        return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        tmp = dict()\n",
    "        for num in nums:\n",
    "            tmp[num] = tmp.get(num, 0) + 1\n",
    "        print(tmp)\n",
    "        length = max([tmp[x] for x in tmp])\n",
    "        arr = []\n",
    "        # for i in range(length):\n",
    "        #     arr.append([])\n",
    "        while tmp:\n",
    "            arr.append(list(tmp))\n",
    "            # for j in range(tmp[num]):\n",
    "            #     arr[i].append(num)\n",
    "            #     i += 1\n",
    "            for x in arr[-1]:\n",
    "                tmp[x] -= 1\n",
    "                if tmp[x] == 0:\n",
    "                    del tmp[x]\n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        d = Counter(nums)\n",
    "        d = {k:v for k,v in d.items()}\n",
    "        res = []\n",
    "        while True:\n",
    "            cur = [k for k,v in d.items() if v>0]\n",
    "            if cur:\n",
    "                res.append(cur)\n",
    "                d = {k:v-1 for k,v in d.items() if v>1}\n",
    "            else:\n",
    "                return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        hashMap = defaultdict(int)\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            if hashMap[num] < len(res):\n",
    "                res[hashMap[num]].append(num)\n",
    "            else:\n",
    "                res.append([num])\n",
    "            hashMap[num] += 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        cnt = collections.Counter(nums).items()\n",
    "        \n",
    "        ans = [[]]\n",
    "        for num, numCnt in cnt:\n",
    "            for i in range(numCnt):\n",
    "                if i==len(ans):\n",
    "                    ans.append([])\n",
    "                ans[i].append(num)\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        a=dict(Counter(nums))\n",
    "        ans=[]\n",
    "        while a:\n",
    "            ans.append(list(a))\n",
    "            for x in ans[-1]:\n",
    "                a[x] -= 1\n",
    "                if a[x] == 0:\n",
    "                    del a[x]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        res = [[]]\n",
    "        flag = True\n",
    "        for i in nums:\n",
    "            n = len(res)\n",
    "            for r in range(n):\n",
    "                if i not in res[r]:\n",
    "                    flag = False\n",
    "                    res[r].append(i)\n",
    "                    break\n",
    "                else:\n",
    "                    flag = True\n",
    "            if flag:\n",
    "                re = []\n",
    "                re.append(i)\n",
    "                res.append(re)\n",
    "            # print(res)\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "\n",
    "        dic = defaultdict(int)\n",
    "\n",
    "        for i in nums:\n",
    "            dic[i] += 1\n",
    "        \n",
    "        print(dic)\n",
    "\n",
    "        res = []\n",
    "        n = max(dic.values())              # need these round\n",
    "\n",
    "        for i in range(n):\n",
    "            temp = []\n",
    "\n",
    "            for key in dic:\n",
    "                if dic[key] >= 1:\n",
    "                    temp.append(key)\n",
    "                    dic[key] -= 1\n",
    "            res.append(temp)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        c = Counter(nums)\n",
    "        ans = []\n",
    "        while c:\n",
    "            l = []\n",
    "            for k in c:\n",
    "                c[k] -= 1\n",
    "                l.append(k)\n",
    "            ans.append(l)\n",
    "            c = +c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        mapping = {}\n",
    "        row_cnt = 0\n",
    "        for num in nums:\n",
    "            if(num not in mapping):\n",
    "                mapping[num] = 1\n",
    "            else:\n",
    "                mapping[num] += 1\n",
    "            row_cnt = max(row_cnt, mapping[num])\n",
    "        result = [[] for _ in range(row_cnt)]\n",
    "        for num in mapping:\n",
    "            for i in range(mapping[num]):\n",
    "                result[i].append(num)\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        cnt = collections.Counter(nums)\n",
    "        sortedCnt = sorted(cnt, key = lambda x: cnt[x], reverse = True)\n",
    "        n = cnt[sortedCnt[0]]\n",
    "        if n ==1:\n",
    "            return [nums]\n",
    "        \n",
    "        ans = [[] for _ in range(n)]\n",
    "        for num in sortedCnt:\n",
    "            for i in range(cnt[num]):\n",
    "                ans[i].append(num)\n",
    "        return ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        cnt = Counter(nums)\n",
    "        ans = [[] for _ in range(max(cnt.values()))]\n",
    "        for x,c in cnt.items():\n",
    "            for i in range(c): ans[i].append(x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        ans = []\n",
    "        cnt = Counter(nums)\n",
    "        while cnt:\n",
    "            ans.append(list(cnt))\n",
    "            for x in ans[-1]:\n",
    "                cnt[x] -= 1\n",
    "                if cnt[x] == 0:\n",
    "                    del cnt[x]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        my_dict = Counter(nums)\n",
    "        rows=max(my_dict.values())\n",
    "        ans=[]\n",
    "        for x in range(rows):\n",
    "            row=[]\n",
    "            for y in my_dict:\n",
    "                if my_dict[y]>0:\n",
    "                    row.append(y)\n",
    "                    my_dict[y]-=1\n",
    "            ans.append(row)\n",
    "        return ans\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        nums.sort()\n",
    "        results = [[]]\n",
    "        idx = 0\n",
    "        cur = None\n",
    "        for num in nums:\n",
    "            if cur != num:\n",
    "                idx = 0\n",
    "                cur = num\n",
    "            if idx < len(results):\n",
    "                results[idx].append(num)\n",
    "            else:\n",
    "                results.append([num])\n",
    "            idx += 1\n",
    "        return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        d = {}\n",
    "        for x in nums:\n",
    "            if d.get(x)==None:\n",
    "                d[x] = 1\n",
    "            else:\n",
    "                d[x] += 1\n",
    "        ret = []\n",
    "        while d:\n",
    "            a = []\n",
    "            k = [i for i in d.keys()]\n",
    "            for x in k:\n",
    "                a.append(x)\n",
    "                if d[x]==1:\n",
    "                    d.pop(x)\n",
    "                else:\n",
    "                    d[x] -= 1\n",
    "            ret.append(a)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        tmp = dict()\n",
    "        for num in nums:\n",
    "            tmp[num] = tmp.get(num, 0) + 1\n",
    "        print(tmp)\n",
    "        length = max([tmp[x] for x in tmp])\n",
    "        arr = []\n",
    "        for i in range(length):\n",
    "            arr.append([])\n",
    "        for num in tmp:\n",
    "            i = 0\n",
    "            for j in range(tmp[num]):\n",
    "                arr[i].append(num)\n",
    "                i += 1\n",
    "        return arr\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        return list(map(lambda x: [i for i in x if i != 114514], zip_longest(*([v] * f for v, f in Counter(nums).items()), fillvalue=114514)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def findMatrix(self, nums: List[int]) -> List[List[int]]:\n",
    "        c = Counter(nums)\n",
    "        ans = sorted(list(c.items()), key=lambda x: x[1], reverse=True)\n",
    "        print(ans)\n",
    "        n = ans[0][1]\n",
    "        \n",
    "        res = [list() for _ in range(n)]\n",
    "        for k, v in ans:\n",
    "            for i in range(v):\n",
    "                res[i].append(k)\n",
    "                \n",
    "        return res\n",
    "                "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
